package com.wql.convert.num2str;

/**
 * 数字转换成汉字
 * 
 * @author Administrator
 * @time 2013年12月25日 下午10:46:54
 * @email hellowql@126.com
 */
public class Num2Str {
	private Num2Str() {
	}

	// 简体中文数字
	private static final String[] ZH_S = "零，一，二，三，四，五，六，七，八，九".split("，");
	// 繁体中文数字
	private static final String[] ZH_T = "零，壹，贰，叁，肆，伍，陆，柒，捌，玖".split("，");
	// 简体中文单位
	private static final String[] UNITS_S = "\"\"，十，百，千，万，十，百，千，亿".split("，");
	// 繁体中文单位
	private static final String[] UNITS_T = "\"\"，拾，佰，仟，万，拾，佰，仟，亿".split("，");
	private static final int UNITS_MAX = 8;// 单位，亿的下标
	private static final int UNITS_MIDDLE = 4;// 单位，万的下标

	/**
	 * 转换为简体汉字
	 * 
	 * @param num
	 *            the number need to convert
	 * @return
	 */
	public static String convert(Long num) {
		return convert(num, false);
	}

	/**
	 * 转换
	 * 
	 * @param num
	 *            the number need to convert
	 * @param traditional
	 *            traditional or simple Chinese
	 * 
	 * @return
	 */
	public static String convert(Long num, boolean traditional) {
		if (null == num) {
			return "";
		}
		int[] reversenums = str2Ints(new StringBuilder(num.toString()).reverse().toString());
		int length = reversenums.length;
		StringBuilder sb = new StringBuilder(length * 2);
		String temporary = "";
		for (int i = 0; i < length; i++) {
			temporary = zh(reversenums[i], traditional);
			// if (ch[0].equals(temporary)) {// 零
			// // 只有万和亿需要带单位
			// if (i % 8 == 4) {
			// temporary += units[i % 8];
			// } else if (i % 8 == 0) {
			// for (int j = 0, k = (i / 8); j < k; j++) {
			// temporary += units[8];
			// }
			// }
			//
			// } else {// 非零
			// if (i % 8 == 0) {
			// for (int j = 0, k = (i / 8); j < k; j++) {
			// temporary += units[8];
			// }
			// } else {
			// temporary += units[i % 8];
			// }
			// }

			if (i % UNITS_MAX == 0) {
				for (int j = 0, k = (i / UNITS_MAX); j < k; j++) {
					temporary += unit(UNITS_MAX, traditional);
				}
			} else if (!(zh(0, traditional).equals(temporary) && (i % UNITS_MAX != UNITS_MIDDLE))) {
				temporary += unit(i % UNITS_MAX, traditional);
			}
			sb.insert(0, temporary);
		}
		return fixed(sb.toString(), traditional);
	}

	/**
	 * 获得指定的进位
	 * 
	 * @param index
	 * @param traditional
	 * @return
	 */
	private static String unit(int index, boolean traditional) {
		return traditional ? UNITS_T[index] : UNITS_S[index];
	}

	/**
	 * 获得指定的中文
	 * 
	 * @param index
	 * @param traditional
	 * @return
	 */
	private static String zh(int index, boolean traditional) {
		return traditional ? ZH_T[index] : ZH_S[index];
	}

	/**
	 * 修正转换后的汉字
	 * 
	 * @param str
	 * @param traditional
	 * @return
	 */
	private static String fixed(String str, boolean traditional) {
		if (null == str || str.length() == 0) {
			return str;
		}
		return traditional ? str.replaceAll("零+(零)", "$1").replaceAll("零+$", "").replaceAll("^壹拾", "拾")
				.replaceAll("零([亿万])", "$1").replaceAll("零壹拾", "零拾") : str.replaceAll("零+(零)", "$1")
				.replaceAll("零+$", "").replaceAll("^一十", "十").replaceAll("零([亿万])", "$1").replaceAll("零一十", "零十");
	}

	/**
	 * 字符串转换成数值数组
	 * 
	 * @param str
	 * @return
	 */
	private static int[] str2Ints(String str) {
		if (null == str || !str.matches("^\\d+$")) {
			return null;
		}
		int[] ints = new int[str.length()];
		for (int i = 0, j = ints.length; i < j; i++) {
			ints[i] = Integer.parseInt(str.substring(i, i + 1));
		}
		return ints;
	}

}
